package com.psdi.element;
import com.psdi.Main;
import com.psdi.manager.GameLoader;
import com.psdi.manager.LevelSystem;
import com.psdi.physics.Collider;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Knife类代表游戏中的刀光元素。
 * 它可以包含刀光的属性和方法，例如位置、方向、攻击力等。
 */
public class Knife extends ElementObject implements Collider {
    private float atk;
    private Vector2D direction; // 子弹方向
    private float animationTime = 0.07f;//动画单图片播放速率
    private enum State { ANIMATING, SHOOTING, COMPLETE }
    private State state = State.ANIMATING;
    private float shootTimer = 0;
    private int shotsCount = 0;
    private int num=0;
    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    public Knife() {
        transform = new Transform();
        tag=Tag.KNIFE;
    }

    @Override
    public void update() {
        switch (state) {
            case ANIMATING:
                if (num < 7) {
                    setImage(GameLoader.iconMap2.get(tag.toString()).get(num));
                    animationTime -= Main.DELTA_TIME;
                    if(animationTime <=0){
                        animationTime = 0.07f;
                        num++;
                    }
                    if(num ==7 && LevelSystem.getINSTANCE().getLevel() <4){
                        setAlive(false);
                        break;
                    }

                } else {
                    state = State.SHOOTING;
                    shootTimer = 0;
                    shotsCount = 0;
                }
                break;
            case SHOOTING:
                shootTimer += Main.DELTA_TIME;
                if (shootTimer >= .15f && LevelSystem.getINSTANCE().getLevel() >=4) {
                    shootTimer = 0;
                    Position airflowPos = new Position(transform.position.x, transform.position.y);
                    GameLoader.LoadKnifeairflow(airflowPos, direction);
                    shotsCount++;
                    if (shotsCount >= LevelSystem.getINSTANCE().getLevel()-3) {
                        state = State.COMPLETE;
                    }
                }
                break;

            case COMPLETE:
                setAlive(false);
                break;
        }
    }
    private int controldir(){
         if (direction.equals(Vector2D.DOWN)) {
            return 90;
        } else if (direction.equals(Vector2D.LEFT)) {
            return 180;
        } else if (direction.equals(Vector2D.UP)) {
            return 270;
        }
         else return 0;
    }
    @Override
    public ElementObject create(String string) {
        String[] split = string.split(";");
        for (String s : split) {
            switch (s.split(":")[0].trim()) {
                case "direction":
                    direction = Vector2D.parseVector2D(s.split(":")[1].trim());
                    break;
                case "x":
                    transform.position.x = Float.parseFloat(s.split(":")[1].trim())-35; // 减去32是为了让刀光的中心对准玩家的中心
                    break;
                case "y":
                    transform.position.y = Float.parseFloat(s.split(":")[1].trim())-35; // 减去32是为了让刀光的中心对准玩家的中心
                    break;
                case "atk":
                    atk = Float.parseFloat(s.split(":")[1].trim());
                    break;
            }
        }
        tag=Tag.KNIFE;
        setImage(GameLoader.iconMap2.get(tag.toString()).get(0));
        transform.position.add(direction.scalarMultiply(50));
        transform.size = new Size(70, 70);
        transform.rotation.z=controldir();
        return this;
    }

    @Override
    public Size colliderSize() {
        return transform.size;
    }

    @Override
    public Position colliderPosition() {
        return transform.position;
    }

    @Override
    public Tag colliderTag() {
        return tag;
    }

    @Override
    public Set<Tag> colliderFilterTag() {
        Set<Tag> tags = new HashSet<>();
        tags.add(Tag.PLAYER);
        tags.add(Tag.BULLET);
        tags.add(Tag.MAP);
        return tags;
    }

    @Override
    public void onCollisionStay(ElementObject other) {
        if(other instanceof Enemy){
            ((Enemy) other).setHp(((Enemy) other).getHp() - atk/3);
            other.transform.position.add(direction.scalarMultiply(15));
        }
        if(other instanceof Boss) {((Boss) other).setHp(((Boss) other).getHp() - atk/3);
        }
    }

}
